package com.carkeeper.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.carkeeper.exception.BussinessException;
import com.carkeeper.mapper.ServiceTypeMapper;
import com.carkeeper.pojo.ServiceTypePO;
import com.carkeeper.service.ServiceTypeService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.StringUtils;


@Service
public class ServiceTypeServiceImpl implements ServiceTypeService{
    @Autowired
    private ServiceTypeMapper serviceTypeMapper;

    @Override
    public Page<ServiceTypePO> query(String code, String name, long page, long pageSize) {
        Page<ServiceTypePO> queryServiceType = new Page<>(page,pageSize);
        //创建分页数据
        QueryWrapper<ServiceTypePO> queryWrapper = new QueryWrapper<>();
        //构建查询条件 根据code name 参数进行查询
        if(!StringUtils.isEmpty(code)){
            queryWrapper.like("code",code);
        }
        if(!StringUtils.isEmpty(name)){
            queryWrapper.like("name",name);
        }
        queryServiceType = serviceTypeMapper.selectPage(queryServiceType, queryWrapper);

        return queryServiceType;
    }

    @Override
    public void add(ServiceTypePO serviceTypePO) {
        QueryWrapper<ServiceTypePO> wrapper = new QueryWrapper<>();
        wrapper
                .lambda()
                .eq(ServiceTypePO::getCode,serviceTypePO.getCode());
        Integer count = serviceTypeMapper.selectCount(wrapper);
        if(count != null && count > 0) throw new BussinessException("服务编码已经存在");
        serviceTypeMapper.insert(serviceTypePO);
    }

    @Override
    public void update(long id,ServiceTypePO serviceTypePO) {
        QueryWrapper<ServiceTypePO> queryWrapper=new QueryWrapper<>();
        queryWrapper.eq("id",id);
        serviceTypeMapper.update(serviceTypePO,queryWrapper);
    }

    @Override
    public void delete(long serviceTypeId) {
        serviceTypeMapper.deleteById(serviceTypeId);
    }

    @Override
    public ServiceTypePO findById(Long id) {
        return serviceTypeMapper.selectById(id);
    }
}
